package com.erpservertwo.controller.login;

import com.erpservertwo.entity.Emp;
import com.erpservertwo.service.LoginService;
import com.erpservertwo.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@Api("登陆相关")
@Slf4j
@RequestMapping("/v2")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("login")
    @ApiOperation("登陆接口")
    public Result<Void> login(@RequestParam String username,
                              @RequestParam String password,
                              HttpSession session) {
        if (username==null || password==null||username.isBlank()||password.isBlank()) {
            return Result.fail();
        }
        Emp emp = loginService.login(username, password);
        if (emp == null) {
            return Result.fail();
        }
        session.setAttribute("emp", emp);
        return Result.success();
    }


}
